package com.rxmvp.baselibrary.dagger;

import android.app.Application;
import android.net.Uri;
import android.util.Log;

import com.jakewharton.picasso.OkHttp3Downloader;
import com.rxmvp.baselibrary.App;
import com.rxmvp.baselibrary.rxretrofit.RetrofitClient;
import com.squareup.picasso.Picasso;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;

/**
 * 作者： huangRZ on 2017/1/21. 15:04
 * 邮箱：917647409@qq.com
 */
@Module
public class NetModule {

    @Singleton
    @Provides
    public Retrofit createRetrofit(Application application){
        return RetrofitClient.getInstance().createClient(application);
    }

    /**
     * picasso with interceptor
     */
    @Provides
    @Singleton
    Picasso providePicasso(App app, OkHttpClient client) {
        return new Picasso.Builder(app)
                .downloader(new OkHttp3Downloader(client))
                .listener(new Picasso.Listener() {
                    @Override
                    public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
                        Log.i("picasso", uri.toString() + "load fail");
                    }
                })
                .build();
    }

    @Provides
    @Singleton
    OkHttpClient provideOkHttpClient(App app){
        return RetrofitClient.getInstance().client(app);
    }
}
